package com.example.controller;


import cn.undraw.util.result.R;
import com.example.domain.dto.CommentDTO;
import com.example.domain.dto.LikedDTO;
import com.example.domain.entity.Comment;
import com.example.domain.entity.User;
import com.example.service.CommentLikeService;
import com.example.service.CommentService;
import com.example.util.AuthUtils;
import com.example.util.page.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author readpage
 * @since 2023-02-03 16:15
 */
@RestController
@Tag(name = "评论管理")
@RequestMapping("/comment")
public class CommentController {
    @Resource
    private CommentService commentService;

    @Resource
    private CommentLikeService commentLikeService;

    @Operation(summary = "根据文章id分页查询评论列表")
    @GetMapping("/page")
    public R<PageInfo<Comment>> page(CommentDTO dto) {
        return R.ok(commentService.pageByAid(dto));
    }

    @Operation(summary = "根据parentId分页查询回复")
    @GetMapping("/replyPage")
    public R<PageInfo<Comment>> replyPageByPid(CommentDTO dto) {
        return R.ok(commentService.replyPageByPid(dto));
    }

    @Operation(summary = "添加评论")
    @PostMapping("/save")
    public R<Comment> save(@RequestBody CommentDTO commentDTO) {
        return R.ok(commentService.save(commentDTO));
    }

    @Operation(summary = "删除评论")
    @DeleteMapping("/remove/{id}")
    public R remove(@PathVariable Integer id) {
        return R.ok(commentService.removeById(id));
    }

    @Operation(summary = "评论点赞")
    @PostMapping("/liked")
    public R liked(@RequestBody LikedDTO likedDTO) {
        User user = AuthUtils.get();
        likedDTO.setUid(user.getId());
        return R.ok(commentLikeService.liked(likedDTO));
    }

    @Operation(summary = "查询当前用户的点赞的评论id")
    @GetMapping("/cidList/{uid}")
    public R<List<Long>> cidList(@PathVariable Long uid) {
        return R.ok(commentLikeService.cidListByUid(uid));
    }
}



